﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using BlueLotus7.ExchangeCore.Utilities;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

namespace BlueLotus7.ExchangeCore
{
    public class DistributorManager
    {
        protected Thread monitorThread = null;
        protected bool runningFlag = false;

        public Dictionary<string, IDistributor> Distributors
        {
            get;
            protected set;
        }

        public DistributorManager()
        {
            this.Distributors = new Dictionary<string, IDistributor>();
        }

        public void Start()
        {
            runningFlag = true;
            if (monitorThread == null)
            {
                monitorThread = new Thread(new ThreadStart(Execute));
                monitorThread.IsBackground = true;
                monitorThread.Start();
            }
        }

        public void Stop()
        {
            runningFlag = false;
        }

        public void Execute()
        {
            while (runningFlag)
            {
                foreach (var key in this.Distributors.Keys)
                {
                    var distributor = this.Distributors[key];

                    if (distributor != null && distributor.IsTimeUp())
                    {
                        Thread t = new Thread(new ParameterizedThreadStart(InvokeRun));
                        t.IsBackground = true;
                        t.Start(distributor);
                    }
                }

                Thread.Sleep(10000);
            }
        }

        public void InvokeRun(object obj)
        {
            if (obj != null)
            {
                IDistributor distributor = obj as IDistributor;
                if (distributor != null)
                {
                    DistributeExecute(distributor);
                }
            }
        }

        public void DistributeExecute(IDistributor distributor)
        {
            if (distributor != null)
            {
                List<ExchangeItem> items = distributor.GetExchangeItems();
                SyncData(items);
            }
        }

        protected static void SyncData(List<ExchangeItem> items)
        {
            if (items != null && items.Count > 0)
            {
                var collection = GetCollection();
                foreach (var one in items)
                {
                    collection.FindAndRemove(Query.And(Query.EQ("CurrencyISO", one.CurrencyISO), Query.EQ("Date", one.Date)), SortBy.Null);
                    collection.Insert(one);
                }
            }
        }

        protected static MongoCollection<ExchangeItem> GetCollection()
        {
            MongoOperator mongoOperator = new MongoOperator();
            return mongoOperator.Database.GetCollection<ExchangeItem>("exchangehistory");
        }
    }
}
